package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.domain.Dept;
import com.itbaizhan.openhis.dto.DeptDto;
import com.itbaizhan.openhis.service.DeptService;
import com.itbaizhan.openhis.mapper.DeptMapper;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author MR
* @description 针对表【sys_dept(部门/科室表)】的数据库操作Service实现
* @createDate 2025-06-06 14:48:09
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>
    implements DeptService{
	
	@Autowired
	private DeptMapper deptMapper;
	
	/**
	 * 分页查询科室
	 * @param deptDto
	 * @return
	 */
	@Override
	public DataGridView listDeptForPage(DeptDto deptDto) {
		
		Page<Dept> page = new Page<>(deptDto.getPageNum(), deptDto.getPageSize());
		QueryWrapper<Dept> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(deptDto.getDeptName()),
				Dept.COL_DEPT_NAME,  deptDto.getDeptName());
		wrapper.eq(StringUtils.isNotBlank(deptDto.getStatus()),
				Dept.COL_STATUS,  deptDto.getStatus());
		wrapper.ge(deptDto.getBeginTime() != null ,
				Dept.COL_CREATE_TIME,  deptDto.getBeginTime());
		wrapper.le(deptDto.getEndTime() != null ,
				Dept.COL_CREATE_TIME,  deptDto.getEndTime());
		wrapper.orderByAsc(Dept.COL_ORDER_NUM);
		deptMapper.selectPage(page, wrapper);
		
		return new DataGridView(page.getTotal(),page.getRecords());
	}
	
	/**
	 * 添加科室
	 * @param deptDto
	 * @return
	 */
	@Override
	public int addDept(DeptDto deptDto) {
		Dept dept = new Dept();
		BeanUtils.copyProperties(deptDto,dept);
		dept.setCreateBy(deptDto.getSimpleUser().getUserName());
		dept.setCreateTime(DateUtil.date());
		return deptMapper.insert(dept);
	}
	
	/**
	 * 修改科室
	 * @param deptDto
	 * @return
	 */
	@Override
	public int updateDept(DeptDto deptDto) {
		Dept dept = new Dept();
		BeanUtils.copyProperties(deptDto,dept);
		dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
		dept.setUpdateTime(DateUtil.date());
		return deptMapper.updateById(dept);
	}
	
	/**
	 * 批量删除科室
	 * @param deptIds
	 * @return
	 */
	@Override
	public int deleteDeptByIds(Long[] deptIds) {
		List<Long> ids = Arrays.asList(deptIds);
		if (ids != null && ids.size() > 0){
			return deptMapper.deleteBatchIds(ids);
		}else{
			return 0;
		}
	}
}




